package com.shenyue.nbtownlet.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.databinding.CheckWorkListItemBinding;
import com.shenyue.nbtownlet.util.StringUtil;

public class CheckWorkViewAdapter extends BaseAdapter {
    private Context mContext;
    private JSONArray items = new JSONArray();
    public interface CheckWorkListItemClickListener{
        void onItemClick(int position);
    }
    private CheckWorkListItemClickListener listener;

    public CheckWorkViewAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            CheckWorkListItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.check_work_list_item, viewGroup, false);
            convertView = binding.getRoot();
            viewHolder = new ViewHolder(binding);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        CheckWorkListItemBinding root = viewHolder.getBinding();
        root.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null)
                    listener.onItemClick(position);
            }
        });

        JSONObject item = items.getJSONObject(position);
        StringBuilder sb = new StringBuilder();
        sb.append(StringUtil.getValue(item.getString("AREANAME")))
                .append(StringUtil.getValue(item.getString("YEAR_MONTH")))
                .append("的考核信息");

        root.TITLE.setText(sb.toString());
        sb.setLength(0);
        sb.append(StringUtil.getValue(item.getString("TOTALSCORE")))
                .append("分");
        root.TOTALSCORE.setText(sb.toString());
        root.CREATORNAME.setText(StringUtil.getValue(item.getString("CREATORNAME")));
        root.STATUS.setText(StringUtil.getValue(item.getString("STATUS")));
        root.CREATETIME.setText(StringUtil.toJSONTime(item.getString("CREATETIME")));


        return convertView;
    }

    public void addItems(JSONArray array) {
        items.addAll(array);
        notifyDataSetChanged();
    }

    public void clear() {
        items.clear();
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(CheckWorkListItemClickListener listener){
        this.listener = listener;
    }

    private class ViewHolder {
        private CheckWorkListItemBinding binding;

        public ViewHolder(CheckWorkListItemBinding binding) {
            this.binding = binding;
        }

        public CheckWorkListItemBinding getBinding() {
            return binding;
        }
    }
}
